package com.njxxted.activiti.common.listener.process;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 描述：执行监听器模板(采用了模版模式)，所有节点执行监听器的实现都必须继承此类
 * <p>
 * 作者：Ostrich Hu
 * 时间：2019/8/9 9:43 星期五
 */
public abstract class BaseTemplateExecutionListener implements ExecutionListener {

    private static final long serialVersionUID = 5801738510129723175L;

    public final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void notify(DelegateExecution execution) {
        ExecutionEntity executionEntity = (ExecutionEntity) execution;
        String activityId = executionEntity.getActivityId();
        String processDefinitionId = executionEntity.getProcessDefinitionId();
        String deploymentId = executionEntity.getDeploymentId();
        //执行实现类业务逻辑
        execute(execution, processDefinitionId, activityId);
       /* //只有节点不为空时，才会去执行脚本。
        if (activityId != null) {
            //获取事件类型
            Integer scriptType = getScriptType();
            //执行事件脚本
            exeEventScript(execution, scriptType, processDefinitionId, activityId);
        }*/
    }

    /**
     * 实现子类的逻辑事务
     *
     * @param execution           委托执行对象
     * @param processDefinitionId 流程定义id
     * @param activityId          节点id
     */
    protected abstract void execute(DelegateExecution execution, String processDefinitionId, String activityId);

    /**
     * 获取脚本类型。
     *
     * @return
     */
    protected abstract Integer getScriptType();


    /**
     * 执行事件脚本。
     *
     * @param execution
     * @param scriptType
     * @param actDefId
     * @param nodeId
     */
    private void exeEventScript(DelegateExecution execution, int scriptType, String actDefId, String nodeId) {
//        BpmNodeScriptService bpmNodeScriptService=(BpmNodeScriptService)AppUtil.getBean("bpmNodeScriptService");
//        BpmNodeScript model=bpmNodeScriptService.getScriptByType(nodeId, actDefId, scriptType);
//        if(model==null) return;
//
//        String script=model.getScript();
//        if(StringUtil.isEmpty(script)) return;
//
//        GroovyScriptEngine scriptEngine=(GroovyScriptEngine)AppUtil.getBean("scriptEngine");
//        Map<String, Object> vars=execution.getVariables();
//        vars.put("execution", execution);
//
//        scriptEngine.execute(script, vars);

        logger.debug("execution script :");
    }
}
